package com.yegs.jltqc.serviceImpl;

import com.yegs.jltqc.TO.MoldPageQuery;
import com.yegs.jltqc.TO.NamePageQuery;
import com.yegs.jltqc.VO.Formwork;
import com.yegs.jltqc.VO.OutCome;
import com.yegs.jltqc.common.StoreException;
import com.yegs.jltqc.config.ConcurentCache;
import com.yegs.jltqc.dao.FormworkRepository;
import com.yegs.jltqc.dao.OutComeRepository;
import com.yegs.jltqc.service.FormworkService;
import com.yegs.jltqc.service.OutComeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class FormworkServiceImpl implements FormworkService {

    @Autowired
    private FormworkRepository formworkRepository;

    @Override
    public Formwork save(Formwork formwork) {
         if(formwork.getId() == null){
             Formwork ent = formworkRepository.findFormworkByNameAndVersion(formwork.getName(), formwork.getVersion());
             if(ent == null){
                 formwork.setCreateTime(new Date());
                 formwork.setCreateBy(ConcurentCache.getUserName());
                 formwork = formworkRepository.save(formwork);
             }else {
                 throw new StoreException(ent.getName() + ":" + ent.getVersion() + "已存在!");
             }
         }else{
             formwork.setUpdateBy(ConcurentCache.getUserName());
             formwork.setCreateTime(new Date());
             formwork = formworkRepository.save(formwork);
         }
        return formwork;
    }

    @Override
    public List<Formwork> list() {
        return formworkRepository.findAll();
    }

    @Override
    public Formwork get(Long id) {
        return formworkRepository.getOne(id);
    }

    @Override
    public List<Formwork> getFormworkByMold(String mold) {
        return formworkRepository.findFormworkByMold(mold);
    }

    @Override
    public void del(Long id) {
        formworkRepository.deleteById(id);
    }

    @Override
    public Page<Formwork> query(MoldPageQuery query) {
        PageRequest request = PageRequest.of(query.getPageIndex() - 1, query.getPageSize());
        if(query.getMold() == null || query.getMold().trim() == ""){
            return formworkRepository.findAll(request);
        }
        if(query.getName() == null || query.getName().trim() == ""){
            return formworkRepository.findFormworkByMold(query.getMold(), request);
        }
        return formworkRepository.findFormworkByNameAndMold(query.getName(), query.getMold(), request);
    }

    @Override
    public Long getNext(Long id, String mold) {
        if(id == null){
            Formwork outCome = formworkRepository.findFirstByMoldOrderByIdDesc(mold);
            if(outCome != null) {
                return outCome.getId();
            }
        }
        else{
            Formwork outCome = formworkRepository.findFirstByIdIsAfterAndMold(id, mold);
            if(outCome == null){ outCome = formworkRepository.findFirstByMoldOrderByIdDesc(mold); }
            if(outCome != null){
                return outCome.getId();
            }
        }
        return null;
    }

    @Override
    public Long getLast(Long id, String mold) {
        if(id == null){
            Formwork outCome = formworkRepository.findFirstByMoldOrderByIdDesc(mold);
            if(outCome != null) {
                return outCome.getId();
            }
        }
        else{
            Formwork outCome = formworkRepository.findFirstByIdBeforeAndMoldOrderByIdDesc(id, mold);
            if(outCome == null){ outCome = formworkRepository.findFirstByMoldOrderById(mold); }
            if(outCome != null){
                return outCome.getId();
            }
        }
        return null;
    }
}
